package com.example.replacebooks.common;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.example.replacebooks.entity.User;
import com.example.replacebooks.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Component
public class JwtTokenUtils {
    private static UserService staticUserServie;
    private static final Logger log = LoggerFactory.getLogger(JwtTokenUtils.class);

    @Resource
    private UserService userService;

    @PostConstruct
    public void setUserService(){
        staticUserServie = userService;
    }

    /*生成token*/
    public static String genToken(String userid, String password) {
        // create 用userid 编码.
        return JWT.create().withAudience(userid)  //将 userid 保存到token里面，作为载荷
                .withExpiresAt(DateUtil.offsetHour(new Date(),2))   //2小时后token过期
                .sign(Algorithm.HMAC256(password)); // password 作为 token 的密钥
    }

    /*获取当前登录的用户信息*/
    public static User getCurrentUser(){
        String token = null;
        try{
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            token = request.getHeader("token");
            if (StrUtil.isBlank(token)){
                token = request.getParameter("token");
            }
            if (StrUtil.isBlank(token)){
                log.error("获取当前登录的token失败，token：{}",token);
                return null;
            }
            //解析token，获取用户名
            String userid = JWT.decode(token).getAudience().get(0);
            return staticUserServie.findById(Integer.valueOf(userid));
        } catch (Exception e){
            log.error("获取当前登录的信息失败，token：{}",token ,e);
            return null;
        }
    }
}
